﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using Microsoft.SqlServer.Management.Common;
using Microsoft.SqlServer.Management.Smo;
using Chi.Lib.DBHelper;
using System.Collections.Generic;

public partial class ShowAllTableSchema : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Database db = DBHelperSMO.GetDBInstance();
        foreach (Microsoft.SqlServer.Management.Smo.Table tb in db.Tables)
        {
            Label lb = new Label();
            lb.ID = "lb_" + tb.ID.ToString();
            lb.Text = "<br>" + tb.Name;

            GridView gv = new GridView();
            gv.ID = tb.Name;
            gv.AutoGenerateColumns = false;
            gv.HeaderStyle.BackColor = gvView.HeaderStyle.BackColor;
            foreach (DataControlField dcf in gvView.Columns)
            {
                gv.Columns.Add(dcf);
            }
            gv.DataSource = GetTableColumns(tb);
            gv.DataBind();

            PlaceHolder1.Controls.Add(lb);
            PlaceHolder1.Controls.Add(gv);
        }


        //int i = 1;
        //DataSet ds = DBClass.getTables();
        //foreach (DataRow dr in ds.Tables[0].Rows)
        //{
        //    Label lb = new Label();
        //    lb.ID = "lb_" + dr["TABLE_NAME"].ToString();
        //    lb.Text = "<br>" + i.ToString() + ". " + dr["TABLE_NAME"].ToString();
        //    GridView gv = new GridView();
        //    gv.ID = dr["TABLE_NAME"].ToString();
        //    gv.AutoGenerateColumns = false;
        //    gv.HeaderStyle.BackColor = gvView.HeaderStyle.BackColor;
        //    foreach (DataControlField dcf in gvView.Columns)
        //    {
        //        gv.Columns.Add(dcf);
        //    }
        //    gv.DataSource = DBClass.getSingleTable(dr["TABLE_NAME"].ToString());
        //    gv.DataBind();

        //    PlaceHolder1.Controls.Add(lb);
        //    PlaceHolder1.Controls.Add(gv);
        //    i++;
        //}
    }

    public List<DBHelperTableSchema> GetTableColumns(Microsoft.SqlServer.Management.Smo.Table table)
    {
        //DBHelperTableSchema

        List<DBHelperTableSchema> SchemaLst = new List<DBHelperTableSchema>();
        foreach (Column col in table.Columns)
        {
            DBHelperTableSchema colSchema = new DBHelperTableSchema();
            colSchema.PK = col.InPrimaryKey ? "*" : "";
            colSchema.ColumnName = col.Name;
            colSchema.DataType = col.DataType.Name;
            colSchema.MaximunLength = col.DataType.MaximumLength.ToString();
            colSchema.NullAble = col.Nullable ? "Null" : "";
            colSchema.Default = col.Default;
            colSchema.Descriptions = col.ExtendedProperties.Contains("MS_Description") ?
                                    col.ExtendedProperties["MS_Description"].Value.ToString() : "";

            SchemaLst.Add(colSchema);
        }

        return SchemaLst;
    }
}
